Skip to content

[MWAR-443] Fix deletion of files placed by maven-dependency-plugin - #642

Open
elharo wants to merge 2 commits into
apache:masterfrom
elharo:MWAR-443-fix-v2
Open

[MWAR-443] Fix deletion of files placed by maven-dependency-plugin#642
elharo wants to merge 2 commits into
apache:masterfrom
elharo:MWAR-443-fix-v2

Conversation

@elharo

@elharo elharo commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Fixes #522

Problem

When maven-dependency-plugin copies provided-scope JARs to WEB-INF/lib/, the WAR plugin's outdated-resource cleanup deletes them because they were not copied by the WAR plugin itself and their timestamps predate the session start time.

Solution

In DefaultWarPackagingContext, skip adding files under WEB-INF/lib/ to outdatedResources unless they match an expected runtime-artifact filename. A new getRuntimeArtifactFileNames() method computes the expected filenames from the set of runtime-scope library artifacts, respecting outputFileNameMapping if configured.

Changes

  • AbstractWarMojo.java: Added getRuntimeArtifactFileNames(); modified FileVisitor to skip marking non-artifact lib files as outdated.
  • AbstractWarPackagingTask.java: Extracted isLibraryType() for reuse.
  • ArtifactsPackagingTask.java: Refactored to use shared isLibraryType().
  • WarExplodedMojoTest.java: Added regression test.

Fixes apache#522

Address code review comments:
- Extract shared isLibraryType() in AbstractWarPackagingTask to
  avoid duplicating artifact type list across the codebase
- Use outputFileNameMapping in getRuntimeArtifactFileNames() when set
- Use isLibraryType() in both ArtifactsPackagingTask and the
  outdated resource detection logic
- Fix test cleanup with try/finally and recursive delete
@elharo
elharo requested a review from Copilot July 28, 2026 18:38
targetFileName = targetFileName.substring(0, targetFileName.lastIndexOf('.')) + ".jar";
}
fileNames.add(targetFileName);
} catch (Exception e) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't need to catch a raw java.lang.Exception here. be more specific about the exception type

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Fixes MWAR-443 by preventing the WAR plugin’s outdated-resource cleanup from deleting dependency JARs in WEB-INF/lib/ that were placed there by maven-dependency-plugin (not by the WAR plugin) when those files predate the session start time.

Changes:

  • Added runtime-artifact filename computation and used it to avoid marking non-runtime lib files as outdated during the cleanup scan.
  • Centralized “library type” detection into a shared isLibraryType() helper and refactored packaging logic to use it.
  • Added a regression test covering the provided-scope dependency-plugin scenario.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
src/main/java/org/apache/maven/plugins/war/AbstractWarMojo.java Avoids deleting WEB-INF/lib files unless they match expected runtime artifact filenames; adds runtime filename computation.
src/main/java/org/apache/maven/plugins/war/packaging/AbstractWarPackagingTask.java Extracts shared isLibraryType() helper for library artifact classification.
src/main/java/org/apache/maven/plugins/war/packaging/ArtifactsPackagingTask.java Refactors artifact copy routing to use isLibraryType() and keeps special handling for par.
src/test/java/org/apache/maven/plugins/war/WarExplodedMojoTest.java Adds MWAR-443 regression coverage to ensure provided-scope JARs aren’t deleted.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 101 to 105
} else if (isLibraryType(type)) {
if ("par".equals(type)) {
targetFileName = targetFileName.substring(0, targetFileName.lastIndexOf('.')) + ".jar";
}
copyFile(id, context, artifact.getFile(), LIB_PATH + targetFileName);
Comment on lines +735 to +738
if ("par".equals(artifact.getType())) {
targetFileName = targetFileName.substring(0, targetFileName.lastIndexOf('.')) + ".jar";
}
fileNames.add(targetFileName);
Comment on lines +684 to +690
if (normalizedPath.startsWith("WEB-INF/lib/")
&& !runtimeArtifactFileNames.contains(
file.toFile().getName())) {
// Skip: file was placed by another plugin, not managed by WAR plugin
} else {
outdatedResources.add(path);
}
Comment on lines +982 to +986
} finally {
// Cleanup
providedJar.delete();
libDir.delete();
}
- Invert the outdated resource detection to protect non-runtime-scope
  library artifacts instead of filtering out runtime-scope artifacts:
  stale files that are not part of the current project (e.g. outdated
  versions of runtime artifacts) are still removed.
- Move the fileNameMapping evaluation and the par to jar conversion
  into shared helpers in AbstractWarPackagingTask and reuse them from
  both the outdated resource detection and ArtifactsPackagingTask.
- Catch InterpolationException instead of Exception when evaluating
  the file name mapping.
- Strengthen the tests: use a real project stub, cover the
  outputFileNameMapping case and the removal of stale runtime files,
  and clean up test resources recursively.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MWAR-443] Maven WAR plugin is deleting files generated by Maven Dependency plugin after upgrading to 3.3.1

2 participants